﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace CompleteReader.Dialogs
{
    public sealed partial class OutlineDialog : CloseableDialog
    {
        public pdftron.PDF.PDFViewCtrl PDFViewCtrl { get; set; }

        public OutlineDialog()
        {
            this.InitializeComponent();
        }

        public override void CloseDialog()
        {
            if (_Opened || _Opening)
            {
                _Closing = true;
                _Closed = false;
                _Opening = false;
                _Opened = false;
                SlideOutPanel.Begin();
            }
        }

        private void SlideOutCompleted(object sender, object e)
        {
            AnnotationGrid.Children.Clear(); //.Clear();
            OutlineGrid.Children.Clear();
            base.CloseDialog();
        }

        public override void OpenDialog()
        {
            if (_Closed || _Closing)
            {
                this.Visibility = Windows.UI.Xaml.Visibility.Visible;
                _Closing = false;
                _Closed = false;
                _Opening = true;
                _Opened = false;
                if (OutlineGrid.Visibility == Windows.UI.Xaml.Visibility.Visible)
                {
                    pdftron.PDF.Tools.Controls.Outline outline = new pdftron.PDF.Tools.Controls.Outline(this.PDFViewCtrl);
                    OutlineGrid.Children.Add(outline);
                    OutlineButton.IsEnabled = false;
                }
                else if (AnnotationGrid.Visibility == Windows.UI.Xaml.Visibility.Visible)
                {
                    pdftron.PDF.Tools.Controls.AnnotationList annotationList = new pdftron.PDF.Tools.Controls.AnnotationList(this.PDFViewCtrl);
                    annotationList.FlashBrush = Application.Current.Resources["ThemeBrushHighlightLight"] as Brush;
                    AnnotationGrid.Children.Add(annotationList);
                    AnnotationsButton.IsEnabled = false;
                }
                SlideInPanel.Begin();
            }
        }

        private void SlideInCompleted(object sender, object e)
        {
            base.OpenDialog();
        }


        private void OutlineButton_Click(object sender, RoutedEventArgs e)
        {
            OutlineButton.IsEnabled = false;
            AnnotationsButton.IsEnabled = true;
            if (OutlineGrid.Children.Count == 0)
            {
                OutlineGrid.Children.Add(new pdftron.PDF.Tools.Controls.Outline(this.PDFViewCtrl));
            }
            ShowOutline();
        }


        private void AnnotationsButton_Click(object sender, RoutedEventArgs e)
        {
            AnnotationsButton.IsEnabled = false;
            OutlineButton.IsEnabled = true;

            if (AnnotationGrid.Children.Count == 0)
            {
                pdftron.PDF.Tools.Controls.AnnotationList annotationList = new pdftron.PDF.Tools.Controls.AnnotationList(this.PDFViewCtrl);
                annotationList.FlashBrush = Application.Current.Resources["ThemeBrushHighlightLight"] as Brush;
                AnnotationGrid.Children.Add(annotationList);
            }
            ShowAnnotationList();
        }

        private void ShowOutline()
        {
            OutlineGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
            AnnotationGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }

        private void ShowAnnotationList()
        {
            AnnotationGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
            OutlineGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }

    }
}
